package com.wechat.service.account.starter.templateMessage;

import com.alibaba.fastjson.JSONObject;
import com.wechat.service.account.dto.messageManage.templateMessage.TemplateMessage;
import com.wechat.service.account.starter.BaseWechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * @author： ygl
 * @date： 2019/07/13
 * @Description：
 * 模板消息
 */
@Slf4j
@Service
public class TemplateMessageService extends BaseWechatService {

    /**
     * 发送模板消息
     * @param templateMessage
     * @return
     */
    public boolean sendTemplate(TemplateMessage templateMessage){
        log.info("********发送模板消息参数:{}********",JSONObject.toJSONString(templateMessage));
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(buildSendTemplateUrl(), templateMessage, JSONObject.class);
        JSONObject body = responseEntity.getBody();
        log.info("********发送模板消息结果:{}********",body);
        if (0 == body.getInteger("errcode")) {
            return true;
        }
        return false;
    }


    public String buildSendTemplateUrl(){
        return "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessTokenService.getAccessToken();
    }
}
